(function($){
	$.fn.wastageInit = function(){
		var $this = $(this);
		var selectRow = null;
		var selectIndex = null;
		var id = $(this).attr('id');
		//编辑
		var editDialog= $('#editDialog',$this);
		//选择商品
		var selectProductDialog = $('#selectProductDialog',$this);
			//查询的商品种类下拉框
		$('#item',selectProductDialog).combobox({
			data:TEA.getProductItemList(),
			valueField:'dataDictionaryID',
			textField:'dataDictionaryName',
			width:150
		})
		//查询
		$('#search',$this).click(function(){
			var queryContent = $('.queryContent',$this);
			var beginDate = $('#beginDate',queryContent).val();
			var endDate = $('#endDate',queryContent).val();
			
			var content = {beginDate:beginDate,endDate:endDate};
			
			$('#wastageList',$this).datagrid({
				queryParams:content,
				pageNumber:1
			});
		})
		//添加
		var onAdd = function(){
			$('#wastagedetailList',editDialog).datagrid({url:TEA.CLEARURL})
			$(editDialog).dialog('open');
		} 
		//删除
		var onDelete = function(){
			if(selectRow==null){
				$.messager.alert("提示","请选择数据行","warning");
				return;
			}
			$.messager.confirm('确认', '确定要删除吗?', function(r){
				if (r){
					var url = 'wastage!delete.action';
					var content = {wastageID:selectRow.wastageID};
					asyncCallService(url,content,function(result){
						if(result.success){
							$.messager.alert("提示","删除成功","info",onReload);
						}else{
							$.messager.alert("提示",result.msg,"warning");
						}
					})
				}
			});
		}
		//刷新列表
		var onReload = function(){
			$('#wastageList',$this).datagrid('reload');
		}
		//加载查询页面列表
		$('#wastageList',$this).datagrid({
			url:"wastage!select.action",
			singleSelect:true,
			fit:true,
			method:"POST",
			nowrap:true,
			striped: true,
			collapsible:true,
			rownumbers:true,
			pagination:true,
			pageSize:30,
			toolbar:[	
						{id:'add',text:'添加',iconCls:'icon-add',handler:function(){onAdd()}},
						{id:'delete',text:'删除',iconCls:'icon-remove',handler:function(){onDelete()}},
						{id:'reload',text:'刷新',iconCls:'icon-reload',handler:function(){onReload()}}
					],
			columns:[[
				{field:'wastageID',hidden:true},
				{field:'productName',title:'品名',width:100,align:"center"},
				{field:'productSize',title:'规格',width:100,align:"center"},
				{field:'unitName',title:'单位',width:100,align:"center"},
				{field:'createDate',title:'日期',width:100,align:"center"},
				{field:'quantity',title:'数量',width:100,align:"center"},
				{field:'price',title:'单价',width:100,align:"center"},
				{field:'amount',title:'金额',width:100,align:"center"},
				{field:'operater',title:'经办人',width:100,align:"center"},
				{field:'note',title:'备注',width:300,align:"center"}
			]],
			onClickRow:function(rowIndex, rowData){
				selectRow = rowData;
				selectIndex = rowIndex;
			},
			onDblClickRow:function(rowIndex,rowData){
				onDetail();
			},
			onLoadSuccess:function(){
				selectRow = null;
		 		selectIndex = null;
			}
		});
		//编辑保存
		var onSave = function(){
			if (lastIndex != null){
				$('#wastagedetailList',editDialog).datagrid('endEdit', lastIndex);
				lastIndex =null;
			}
			var rows = $('#wastagedetailList',editDialog).datagrid('getRows');
			if(rows.length==0){
				$.messager.alert('提示','至少包含一件商品','warning');
				return;
			}
			var productIDArray = new Array(); 
			var quantityArray = new Array(); 
			var priceArray = new Array(); 
			var noteArray = new Array(); 
			
			$(rows).each(function(index,item){
				productIDArray.push(item.productID);
				var quantity = item.quantity;
				if(quantity==null||$.trim(quantity)==''){
					quantity=0;
				}
				quantityArray.push(quantity);
				var price = item.price;
				if(price==null||$.trim(price)==''){
					price=0;
				}
				priceArray.push(price);
				var note = item.note;
				if($.trim(note)==''){
					note ='*';
				}
				noteArray.push(note);
			})
			
			var url = 'wastage!add.action';
			$('#productids',editDialog).val(productIDArray.join(CSIT.join));
			$('#qtys',editDialog).val(quantityArray.join(CSIT.join));
			$('#prices',editDialog).val(priceArray.join(CSIT.join));
			$('#notes',editDialog).val(noteArray.join(CSIT.join));
			
			$('#wastageForm',editDialog).ajaxSubmit({
				url:url,
				type:'post',
				dataType:'json',
				success:function(result){	
					if(result.success){
						var fn = function(){
							onReload();
							onExit();
						}
						$.messager.alert('提示','保存成功','info',fn);
					}else{
						$.messager.alert('提示',result.msg,"warning");
					}
				}
			});
		}
		//编辑页面退出
		var onExit = function(){
			$(editDialog).dialog('close');
			lastIndex = null;
			type=0;
			$('#saleForm',editDialog).form('clear');
		}
		
		$(editDialog).dialog({  
		    title: '编辑损耗单',  
		    width:970,
		    height:570,
		    closed: true,  
		    cache: false,  
		    modal: true,
		    closable:false,
		    toolbar:[{text:'保存',iconCls:'icon-save',handler:function(){onSave()}},'-',
		    		 {text:'退出',iconCls:'icon-exit',handler:function(){onExit()}
			}]
		});
		var onEditAdd = function(){
			$(selectProductDialog).dialog('open');
		}
		var onEditDelete = function(){
			var row = $('#wastagedetailList',editDialog).datagrid('getSelected');
			if(row==null){
				$.messager.alert("提示","请选择数据行","warning");
				return;
			}
			$.messager.confirm('确认', '确定要删除吗?', function(r){
				if (r){
					var index = $('#wastagedetailList',editDialog).datagrid('getRowIndex',row);
					$('#wastagedetailList',editDialog).datagrid('deleteRow',index);
				}
			});
		}
		var lastIndex = null;
		//加载编辑页面列表
		$('#wastagedetailList',editDialog).datagrid({
			singleSelect:true,
			fitColumns:true,
			method:"POST",
			nowrap:true,
			striped: true,
			collapsible:true,
			rownumbers:true,
			pagination:false,
			height:480,
			width:950,
			toolbar:[	
						{id:'add',text:'添加',iconCls:'icon-add',handler:function(){onEditAdd()}},
						{id:'delete',text:'删除',iconCls:'icon-remove',handler:function(){onEditDelete()}}
					],
			columns:[[
				{field:'productID',hidden:true},
				{field:'productName',title:'品名',width:100,align:"center"},
				{field:'productSize',title:'规格',width:100,align:"center"},
				{field:'unitName',title:'单位',width:100,align:"center"},
				{field:'quantity',title:'数量',width:100,align:"center",editor:{type:'numberbox',options:{precision:2}}},
				{field:'price',title:'单价',width:100,align:"center",editor:{type:'numberbox',options:{precision:2}}},
				{field:'amount',title:'金额',width:100,align:"center"},
				{field:'note',title:'备注',width:300,align:"center",editor:{type:'text'}}
			]],
			onClickRow:function(rowIndex, rowData){
				if (lastIndex != rowIndex){
					$(this).datagrid('endEdit', lastIndex);
					$(this).datagrid('beginEdit', rowIndex);
				}
				lastIndex = rowIndex;
			},
			onAfterEdit:function(rowIndex, rowData, changes){
				var amount = parseFloat(rowData.quantity)*parseFloat(rowData.price);
				if(isNaN(amount)){
					amount = 0
				}
				rowData.amount = amount.toFixed(2);
				$(this).datagrid('updateRow', {index:rowIndex,row:rowData});
			}
		});
		
		var onSelect = function(){
			var rows = $('#productList',selectProductDialog).datagrid('getSelections');
			if(rows.length==null){
				$.messager.alert("提示","请选择数据行","warning");
				return;
			}
			$(rows).each(function(index,selectrow){
				var row = {productID:selectrow.productID,productName:selectrow.productName,
				       productSize:selectrow.productSize,unitName:selectrow.unitName,quantity:0,
				       price:selectrow.productPrice,amount:0,note:''};
				$('#wastagedetailList',editDialog).datagrid('appendRow',row);
			})
			onSelectExit();
		}
		
		var onSelectExit = function(){
			$(selectProductDialog).dialog('close');
		}
		
		$(selectProductDialog).dialog({  
		    title: '选择商品',  
		    width:900,
		    height:550,
		    closed: true,  
		    cache: false,  
		    modal: true,
		    closable:false,
		    toolbar:[{text:'选择',iconCls:'icon-ok',handler:function(){onSelect()}},'-',
		    		 {text:'退出',iconCls:'icon-exit',handler:function(){onSelectExit()}}]
		});
		//选择页面查询
		$('#selectProductSearch',selectProductDialog).click(function(){
			var productItemID = $('#item',selectProductDialog).combobox('getValue');
			var productCode = $('#code',selectProductDialog).val();
			var productName = $('#name',selectProductDialog).val();
			
			var content = {productCode:productCode,productName:productName,productItemID:productItemID};
			
			$('#productList',selectProductDialog).datagrid({
				url:"product!select.action",
				queryParams:content,
				pageNumber:1
			});
		})
		//加载商品列表
		$('#productList',selectProductDialog).datagrid({
			singleSelect:false,
			fitColumns:true,
			method:"POST",
			nowrap:true,
			striped: true,
			collapsible:true,
			rownumbers:true,
			pagination:true,
			pageSize:30,
			height:450,
			width:880,
			columns:[[
				{field:'productID',checkbox:true},
				{field:'productCode',title:'商品编号',width:100,align:"center"},
				{field:'productName',title:'商品名称',width:200,align:"center"},
				{field:'productSize',title:'规格',width:100,align:"center"},
				{field:'productPrice',title:'单价',width:100,align:"center"},
				{field:'unitName',title:'单位',width:100,align:"center"}
			]]
		});
	}
})(jQuery);